package com.baidu.process.chat.core.web.controller;

import com.baidu.process.chat.core.web.bean.eval.EvalResult;
import com.baidu.process.chat.core.web.bean.eval.EvalVO;
import com.baidu.process.chat.core.web.service.IEvaluationService;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

/**
 * 满意度调研
 *
 * @author lws
 * @date 2022-11-08
 */
@RestController
@RequestMapping("/adapter/unit")
public class EvaluationController {

    private final IEvaluationService evalResultService;

    public EvaluationController(IEvaluationService evalResultService) {
        this.evalResultService = evalResultService;
    }

    /**
     * 推送满意度调研
     * channel=0:微信公众号 channel=1:沃世界APP
     * 1.app渠道：推送固定json
     * 2.wxApp渠道：推送url
     *
     * @param vo 满意度调研参数
     * @return
     */
    @PostMapping("/pushEval")
    public EvalResult pushEval(@RequestBody EvalVO vo) {
        try {
            return evalResultService.pushEval(vo);
        } catch (Exception e) {
            return EvalResult.fail(e.getMessage());
        }
    }

    /**
     * 获取session对应数据
     *
     * @param sessionId
     * @return
     */
    @GetMapping("/getData")
    public EvalResult getData(@RequestParam(name = "sessionId") String sessionId) {
        try {
            return evalResultService.getData(sessionId);
        } catch (Exception e) {
            return EvalResult.fail(e.getMessage());
        }
    }

    /**
     * 满意度调查结果入库
     *
     * @param vo 满意度调研参数
     * @return
     */
    @PostMapping("/recordEvalResult")
    public EvalResult recordEvalResult(@RequestBody EvalVO vo) {
        try {
            return evalResultService.recordEvalResult(vo);
        } catch (Exception e) {
            // sessionId唯一键冲突
            if (e instanceof DuplicateKeyException) {
                return EvalResult.fail("会话编号重复：" + vo.getSessionId());
            }
            return EvalResult.fail(e.getMessage());
        }
    }

    /**
     * 删除sessionId过期数据
     *
     * @param vo
     * @return
     */
    @PostMapping("/del")
    public EvalResult del(@RequestBody EvalVO vo) {
        try {
            return evalResultService.del(vo.getSessionId());
        } catch (Exception e) {
            return EvalResult.fail(e.getMessage());
        }
    }
}
